<?php
/**
 * PHP Daycare
 * This is our install file. It will setup the database for the user.
 * @author Jason Butz
 * @version 0.0
 * @package PHPDaycare
 */

include_once("config.php");
include_once("functions.php");
/**************************/
$sql[] = "CREATE TABLE IF NOT EXISTS `adults` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `phone` varchar(12) NOT NULL,
  `d_license` varchar(255) NOT NULL,
  `photo` blob,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;";
$sql[] = "CREATE TABLE IF NOT EXISTS `attendance` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `child_id` int(11) NOT NULL,
  `class_id` int(11) NOT NULL,
  `parent_in` int(11) NOT NULL,
  `parent_out` int(11) DEFAULT NULL,
  `in` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `out` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;";
$sql[] = "CREATE TABLE IF NOT EXISTS `child` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `phone` varchar(12) NOT NULL,
  `allergies` text,
  `med_cond` text,
  `notes` text,
  `photo` blob,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;";
$sql[] = "CREATE TABLE IF NOT EXISTS `class` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `location` varchar(255) NOT NULL,
  `desc` text,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;";
$sql[] = "CREATE TABLE IF NOT EXISTS `c_a_rel` (
  `child` int(11) NOT NULL,
  `adults` int(11) NOT NULL,
  PRIMARY KEY (`child`,`adults`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
$sql[] = "CREATE TABLE IF NOT EXISTS `event` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `ename` varchar(255) NOT NULL,
  `edesc` text NOT NULL,
  `etime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=120 ;";
$sql[] = "CREATE TABLE IF NOT EXISTS `worker` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  `misc_info` text,
  `username` varchar(20) NOT NULL,
  `password` tinytext NOT NULL,
  `administrator` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;";
/**************************/
if(!file_exists(".install.lock"))
{
	if(!isset($_POST['user']))
	{
	?>
		<p>Welcome to the installation assistant for PHPDaycare. Please confirm all of the information below and give the requested admin user information. Also be sure the directory this file is in has write permissions on it (777 in Linux). This can be changed after the install.</p>

		<p>Database name: <? echo $config->db_name; ?><br />Database username: <? echo $config->db_user; ?> </p>
		<form method="post" action="install.php">
		  <p>
			 <label>Username
				<input type="text" name="user"/>
			 </label>
		  </p>
		  <p>
			 <label>Password
				<input type="password" name="pass"/>
			 </label>
		  </p>
			<p>
			 <label>Password (Again)
				<input type="password" name="pass2"/>
			 </label>
		  </p>
			 <input type="submit" name="button" value="Install" />
		  </p>
		</form>
	<?
	}
	else
	{
		if($_POST['pass'] != $_POST['pass2'])
		{
			die("Error: The passwords did not match. Please refresh the page and try again.");
		}
		else
		{
			// We can now start the install
			$r = $db->query('BEGIN');
			foreach($sql as $q)
			{
				$r = $db->query($q);
				if(!$r)
				{
					$r = $db->query('ROLLBACK');
					die("An error occured during the database install please be sure that all the information in config.php is correct");
				}
			}
			$r = $db->query('COMMIT');
			$user = mysql_escape_string($_POST['user']);
			$pass = mysql_escape_string($_POST['pass']);
			$r = $db->query("INSERT INTO `worker` 
							(`username`, `password`, `administrator`) 
							VALUES 
							('$user', PASSWORD('$pass'), '1');");
			if(!$r)
			{
				die("Error: The database was setup but the administrator account was not added properly. Please try this again.");
			}
			touch('.install.lock');
			echo '<p>You may remove install.php now if you so wish. The installation is complete if you do not see any errors.</p>';
		}
	}
}
else
{
	?>
	<p>Error: The installer is locked. Please remove the file <i>.install.lock</i> to proceed with installation.</p>
	<?
}
